<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑商品</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }

        h2 {
            text-align: center;
            color: #333;
        }

        label {
            display: block;
            margin: 10px 0 5px;
            font-size: 14px;
            color: #555;
        }

        input[type="text"],
        input[type="number"],
        textarea,
        select {
            width: 100%;
            padding: 10px;
            margin: 8px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }

        textarea {
            resize: vertical;
            min-height: 100px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #42b983;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #388e64;
        }

        .error {
            color: red;
            font-size: 12px;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>编辑商品信息</h2>
    <form id="editForm">
        <input type="hidden" id="id" name="id">
        <label for="title">商品名称</label>
        <input type="text" id="title" name="title" required>

        <label for="price">商品价格</label>
        <input type="number" id="price" name="price" required>

        <label for="descr">商品描述</label>
        <textarea id="descr" name="descr" required></textarea>

        <label for="img">商品图片地址</label>
        <input type="text" id="img" name="img" required>

        <label for="state">商品状态</label>
        <select id="state" name="state">
            <option value="0">可销售</option>
            <option value="1">不可销售</option>
        </select>

        <button type="button" onclick="submitForm()">保存</button>
    </form>
</div>

<script>
    // 获取 URL 参数
    function getParam(name) {
        const url = new URL(window.location.href);
        return url.searchParams.get(name);
    }

    // 页面加载时获取原始数据
    $(function () {
        let id = getParam("id");
        $.get("getGoodById", {id: id}, function (res) {
            if (res.code === 200) {
                let good = res.data;
                $("#id").val(good.id);
                $("#title").val(good.title);
                $("#price").val(good.price);
                $("#descr").val(good.descr);
                $("#img").val(good.img);
                $("#state").val(good.state);
            } else {
                alert("加载商品失败");
            }
        }, "json");
    });

    function submitForm() {
        let data = $("#editForm").serialize();
        console.log("提交的数据:", data);
        $.post("updateGood", data, function (res) {
            if (res === "Y") {
                alert("修改成功！");
                // 返回列表

            } else {
                alert("修改成功！");
                window.location.href = "allgoods.html";
            }
        });
    }
</script>
</body>
</html>
